//
//  NewsDetailViewController.m
//  HWSGW
//
//  Created by avachen on 15/10/31.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "NewsDetailViewController.h"

@interface NewsDetailViewController ()

@end

@implementation NewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    
    
//    //1、加载网络URL
//    NSURL *url = [NSURL URLWithString:@"http://www.youku.com"];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    
//    [webView loadRequest:request];
//
    
//    //2、加载本地的HTML代码
//    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"优酷" ofType:@"html"];
////    NSLog(@"%@", filePath);
//    
//    NSString *s = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
////    NSLog(@"%@", s);
    
   
    [self _loadData];
    
    [self.view addSubview:_webView];
    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [_activityView stopAnimating];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:_activityView];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    
}
- (void)_loadData{
    NSDictionary *jsonData = [HWDataService requestData:news_detail];
    NSString *content = [jsonData objectForKey:@"content"]; //新闻内容
    NSString *source = [jsonData objectForKey:@"source"]; //新闻来源
    NSString *time = [jsonData objectForKey:@"time"]; //新闻时间
    NSString *author = [jsonData objectForKey:@"author"]; //新闻作者
    NSString *title = [jsonData objectForKey:@"title"]; //新闻标题
    //2、加载本地的HTML代码
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"news" ofType:@"html"];
    //    NSLog(@"%@", filePath);
    
    NSString *html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    
    //子标题：来源 时间
    NSString *subTitle = [NSString stringWithFormat:@"%@ %@", source, time];
    //编辑（作者）
    NSString *at = [NSString stringWithFormat:@"编辑（%@）", author];
    
    NSString *htmlString = [NSString stringWithFormat:html, title, subTitle, content, at];
    
    [_webView loadHTMLString:htmlString baseURL:nil];
    
    //自适应
    _webView.scalesPageToFit = YES;
    
    _webView.delegate = self;
    
    
}

//1、webView 开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_activityView startAnimating];
    
}
//2、webView结束加载
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activityView stopAnimating];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
